package com.hsh.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.erp.domain.common.Result;
import com.hsh.erp.domain.dto.InOutTypeDTO;
import com.hsh.erp.domain.entity.InOutType;
import com.hsh.erp.domain.vo.InOutTypeVO;
import com.hsh.erp.init.DataInitializer;
import com.hsh.erp.mapper.InOutTypeMapper;
import com.hsh.erp.service.InOutTypeService;
import com.hsh.erp.utils.MyBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author USER
 * @description 针对表【in_out_bound_type】的数据库操作Service实现
 * @createDate 2024-05-30 16:21:43
 */
@Service
public class InOutTypeServiceImpl extends ServiceImpl<InOutTypeMapper, InOutType>
        implements InOutTypeService {
    @Resource
    private DataInitializer dataInitializer;

    /**
     * 获得出入库类型，null-所有 0-入库类型 1-出库类型
     *
     * @param inOutTypeVO
     * @return
     */
    @Override
    public Result getAll(InOutTypeVO inOutTypeVO) {
        List<InOutTypeDTO> inOutTypeDTOList = new ArrayList<>();
        Integer inOrOut = inOutTypeVO.getInOrOut();
        if (inOrOut == null) {
        } else if (inOrOut == 1) {// 入库
            inOutTypeDTOList = DataInitializer.getInTypeList();
        } else if (inOrOut == 2) {// 出库
            inOutTypeDTOList = DataInitializer.getOutTypeList();
        }
        // 本地缓存没有时，更新缓存并获取
        if (inOutTypeDTOList.isEmpty()) {
            inOutTypeDTOList = dataInitializer.initInOutType(inOrOut);
        }
        return Result.success(inOutTypeDTOList);
    }

    /**
     * 列表查询
     *
     * @param inOutTypeVO
     * @return
     */
    @Override
    public Result<List<InOutTypeDTO>> getList(InOutTypeVO inOutTypeVO) {
        Wrapper wrapper = new QueryWrapper();
        List<InOutTypeDTO> inOutTypeDTOList
                = MyBeanUtils.copyList(list(wrapper), InOutTypeDTO.class);
        return Result.success(inOutTypeDTOList);
    }

    /**
     * 修改一条记录
     *
     * @param inOutTypeVO
     * @return
     */
    @Override
    public Result updateOne(InOutTypeVO inOutTypeVO) {
        boolean update = updateById(MyBeanUtils.copy(inOutTypeVO, InOutType.class));
        dataInitializer.initInOutType();
        return Result.create(update);
    }

    /**
     * 删除一条记录
     *
     * @param inOutTypeVO
     * @return
     */
    @Override
    public Result deleteOne(InOutTypeVO inOutTypeVO) {
        return updateOne(inOutTypeVO);
    }

    /**
     * 新增一条记录
     *
     * @param inOutTypeVO
     * @return
     */
    @Override
    public Result addOne(InOutTypeVO inOutTypeVO) {
        boolean save = save(MyBeanUtils.copy(inOutTypeVO, InOutType.class));
        dataInitializer.initInOutType();
        return Result.create(save);
    }


    /**
     * 根据id获取名称
     *
     * @param id
     * @return
     */
    @Override
    public String getNameById(Integer id) {
        // 获取缓存
        Map<Integer, String> inOutTypeMap = DataInitializer.getInOutTypeMap();
        // 通过数据库获取并写到缓存，再次获取缓存
        if (inOutTypeMap == null || inOutTypeMap.isEmpty()) {
            inOutTypeMap = dataInitializer.initInOutType();
        }
        return inOutTypeMap.get(id);
    }

    //============================================私有方法开始============================================
    //============================================私有方法结束============================================
}




